<template>
  <div class="page">
    <h2>{{topic.title}}</h2>
    <div class="author-info">
      <img :src="topic.author.avatar_url" />
      <div class="info">
        <p class="top">
          <span class="author">{{topic.author.loginname}}</span>
          <span class="right">
            <span class="topping" v-show="topic.top">置顶</span>
            <span class="share" v-show="(topic.tab === 'share' && !topic.top ? true :false)">分享</span>
            <span class="pyr" v-show="(topic.tab === 'ask' && !topic.top ? true :false)">问答</span>
          </span>
        </p>
        <p class="bottom">
          <span>发布于:{{topic.create_at}}天前</span>
          <span>{{topic.visit_count}}次浏览</span>
        </p>
      </div>
    </div>
    <div class="topic-content" v-html="topic.content"></div>
  </div>
</template>

<script>
import { getTopics } from "@/request/api";
export default {
  // "topic",
  data() {
    return {
      tid: this.$route.params.tid,
      topic: {}
    }
  },
  // props: ["tid"],
  created() {
    console.log(this.tid);
    getTopics().then(res => {
      // console.log(res);
      const data = res.data;
      data.forEach(element => {
        if(element.id === this.tid) {
          this.topic = element;
          // console.log(this.topic);
          return
        }
      });
    });
    // this.$store.commit('getTopic', this.tid);
    // console.log(this.$store.state.topic);
  }
};
</script>

<style lang="less">
.page {
  width: 100%;
  height: auto;
}
h2 {
  padding: 5px;
  margin: 15px;
  font-size: 18px;
  color: #2c3e50;
  background: #f0f0f0;
  line-height: 1.5;
  border-radius: 5px;
}
.author-info {
  display: flex;
  padding: 0 15px;
  box-sizing: border-box;
  img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid #f3f3f3;
    margin-right: 10px;
  }
  .info {
    flex: 1;
    color: #34495e;
    font-size: 12px;
    p {
      display: flex;
      justify-content: space-between;
      align-items: center;
      &.top {
        padding: 3px 0;
      }
    }
    .right {
      .share,
      .pyr,
      .topping {
        display: inline-block;
        padding: 5px 22px;
        background: #3498db;
        text-align: center;
        line-height: 1;
        color: #fff;
        font-size: 10px;
        font-weight: 400;
        border-radius: 4px;
      }
      .topping {
        background: #e74c3c;
      }
      .share {
        background: #1abc9c;
      }
    }
  }
}
.topic-content {
  padding: 15px;
  margin-top: 15px;
  color: #333;
  line-height: 1.6;
  word-wrap: break-word;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  .markdown-text {
  }
  img {
    width: 100%;
    height: auto;
  }
}
</style>